<template>
  <div class='dashboard-container'>
    <div class="app-container">
      <el-card shadow="never">
        <!-- 筛选 -->
        <el-row type="flex" justify="space-between" class="selectForm">
          <div class="grid-content bg-purple">
               <el-form
               :inline="true"
               :model="data"
               class="demo-form-inline"
               >
                  <el-form-item label="目录名称">
                 <el-input
                 v-model="data.directoryName"
                 placeholder
                 size="small"
                 ></el-input>
                   </el-form-item>
                <el-form-item label="状态"  style="margin-left: 40px">
              <el-select
               v-model="data.state"
                placeholder="请选择"
                 size="small">
             <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value"></el-option>
               </el-select>
                </el-form-item>
                  <el-form-item >
                  <el-button  size="small" @click="reset">清除</el-button>
                  <el-button size="small" type="primary" @click="search" >搜索</el-button>
                </el-form-item>
             </el-form>
          </div>

            <div class="grid-content bg-purple">
                <el-button v-if="$route.query.id" type="text" @click="$router.push('/subjects/list')">
                <i class="el-icon-back">返回学科</i>
              </el-button>
              <el-button type="success" size="small" @click="add">
                <i class="el-icon-edit"></i>新增标签</el-button>
            </div>

          </el-row>
            <el-alert
          :title="`数据一共${total}条`"
          type="info"
          show-icon
          :closable="false"
          style="margin-bottom:20px"
        ></el-alert>
      <!-- 下表格区域 -->
         <el-table :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" width="100px">
      </el-table-column>
      <el-table-column prop="subjectName" label="所属学科" >
      </el-table-column>
      <el-table-column prop="directoryName" label="目录名称">
      </el-table-column>
      <el-table-column prop="username" label="创建者">
      </el-table-column>
      <el-table-column prop="addDate" label="创建日期" :formatter="formatterDate" min-width="100px">

      </el-table-column>
      <el-table-column prop="totals" label="面试题数量">
      </el-table-column>
      <el-table-column prop="state" label="状态">
        <template #default="{row}">
          {{ row.state ? '已启用' : '已禁用' }}
        </template>
      </el-table-column>
       <el-table-column  label="操作">
         <template #default="{row}">
          <el-button type="text" size="medium" @click="handleState(row)">{{ row.state ? '禁用' : '启用'}}</el-button>
          <el-button type="text" size="medium" :disabled="row.state===1" @click="edit(row.id)">修改</el-button>
          <el-button type="text" size="medium" :disabled="row.state===1" @click="del(row.id)">删除</el-button>
         </template>
      </el-table-column>
    </el-table>
    <!-- 分页功能 -->
     <el-pagination
        background
        style="margin-top: 20px;text-align: right"
         :current-page="data.page"
          :page-sizes="[5, 10, 50, 100]"
          :page-size="data.pagesize"
          :hide-on-single-page="this.list.length <= 1"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        layout="  prev, pager, next, sizes, jumper"
        :total="total">
      </el-pagination>
      </el-card>
       <!-- 新增目录的对话框 -->
      <DirectorysAdd ref="dirAdd" :show-dialog.sync="showDialog" @add="getDirectorys"></DirectorysAdd>
    </div>
  </div>
</template>

<script>
import { list as getCompanys } from '@/api/hmmm/companys'
import { list as getQuestion } from '@/api/hmmm/questions'
import { list, remove, changeState } from '@/api/hmmm/directorys.js'
import DirectorysAdd from '../components/directorys-add.vue'
import { status } from '@/api/hmmm/constants.js'
import dayjs from 'dayjs'

export default {
  name: 'directorys',
  components: {
    DirectorysAdd
  },
  data () {
    return {
      list: [],
      total: 0,
      data: {
        page: 1,
        pagesize: 10,
        subjectID: null,
        directoryName: null,
        state: null
      },
      showDialog: false,
      status: status
    }
  },
  async created () {
    getCompanys()

    getQuestion()
    if (this.$route.query.id) {
      const { data: { items, counts } } = await list({ page: this.page, pagesize: this.pagesize, subjectID: this.$route.query.id })
      // this.loading = false
      this.list = items
      this.total = counts
    } else {
      // 1.进页面就获取列表
      this.getDirectorys()
    }
  },
  methods: {
    // 获取学科目录数据
    async getDirectorys () {
      const { data: { items, counts } } = await list(this.data)
      this.list = items
      console.log(items)
      this.total = counts
    },
    // 切换一页显示多少
    handleSizeChange (val) {
      this.data.pagesize = val
      this.data.page = 1
      this.getDirectorys()
    },
    // 切换页
    handleCurrentChange (val) {
      // console.log(val, '888')
      this.data.page = val
      this.getDirectorys()
    },
    // 删除
    del (id) {
      this.$confirm('此操作将永久删除该标签, 是否继续?', '温馨提示', { type: 'warning' }).then(async () => {
        await remove({ id })
        if (this.list.length === 1 && this.data.page > 1) {
          this.data.page--
        }

        this.$message.success('删除成功')
        this.getDirectorys()
      })
    },
    // 点击新增标签显示弹窗
    add () {
      this.showDialog = true
    },
    // 点击修改打开弹框,传递数据id
    edit (id) {
      this.showDialog = true
      this.$refs.dirAdd.getDetail({ id })
    },
    // 处理状态按钮
    async handleState (row) {
      console.log(row.state)
      if (row.state === 0) {
        row.state = 1
      } else {
        row.state = 0
      }
      await changeState({ id: row.id, state: row.state })
      this.$message.success('操作成功')
    },
    // 搜索按钮
    search () {
      this.getDirectorys()
    },
    // 清楚按钮
    reset () {
      this.data = {
        page: 1,
        pagesize: 10,
        subjectID: null,
        directoryName: null,
        state: null
      }
      this.getDirectorys()
    },
    // 处理时间格式化
    formatterDate (cellValue) {
      // console.log(cellValue.addDate)
      return dayjs(cellValue.addDate).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang='less'></style>
